﻿using System;
using System.Collections.ObjectModel;
using System.Data.SQLite;
using System.Windows;
using System.Windows.Controls;
using WpfApp1.Model;
using WpfApp1.ViewModel;

namespace WpfApp1.View
{
    /// <summary>
    /// TripList.xaml 的交互逻辑
    /// </summary>
    public partial class TripList : UserControl
    {
        public TripList()
        {
            InitializeComponent();
            TripsViewModel trips = new TripsViewModel();
            DataContext = trips;
        }

        // 编辑
        private void List_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            if (!(e.Row.Item is TripModel trip))
            {
                return;
            }

            if (trip.Title == null && trip.Start_time == null && trip.End_time == null && trip.Area == null && trip.Car_cost == null && trip.Hotel_cost == null && trip.Remark == null)
            {
                return;
            }

            if (trip.Start_time.ToString().StartsWith("0001/1/1") || trip.End_time.ToString().StartsWith("0001/1/1"))
            {
                MessageBox.Show("日期填写不正确");
                return;
            }

            float carCost = Convert.ToSingle(trip.Car_cost != null && trip.Car_cost.Length > 0 ? trip.Car_cost : "0");
            float hotelCost = Convert.ToSingle(trip.Hotel_cost != null && trip.Hotel_cost.Length > 0 ? trip.Hotel_cost : "0");
            if (carCost > 0 || hotelCost > 0)
            {
                trip.Total_cost = (carCost + hotelCost).ToString();
            }

            string sql;
            if (trip.Id == 0)
            {
                sql = "insert into trip (title,start_time,end_time,area,car_cost,hotel_cost,total_cost,remark) values (\"" + trip.Title + "\",\"" + trip.Start_time + "\",\"" + trip.End_time + "\",\"" + trip.Area + "\",\""
                    + trip.Car_cost + "\",\"" + trip.Hotel_cost + "\",\"" + trip.Total_cost + "\",\"" + trip.Remark + "\")";
            }
            else
            {
                sql = "update trip set title=\"" + trip.Title + "\",start_time=\"" + trip.Start_time + "\",end_time=\"" + trip.End_time + "\",area=\"" + trip.Area + "\",car_cost=\"" + trip.Car_cost + "\",hotel_cost=\""
                    + trip.Hotel_cost + "\",total_cost=\"" + trip.Total_cost + "\",remark=\"" + trip.Remark + "\" where id=" + trip.Id;
            }

            SQLiteCommand command = new SQLiteCommand(sql, MainWindow.connecttion);
            command.ExecuteNonQuery();

            string sql_id = "select last_insert_rowid() from trip";
            command = new SQLiteCommand(sql_id, MainWindow.connecttion);
            var id = command.ExecuteScalar();
            if (trip.Id == 0)
            {
                trip.Id = Convert.ToInt64(id);
            }

        }

        // 此时未生成记录，无法赋值
        private void List_AddingNewItem(object sender, AddingNewItemEventArgs e)
        {

        }

        // 添加新记录时默认时间
        private void List_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
        {
            TripModel trip = e.Row.Item as TripModel;
            trip.Start_time = DateTime.Now;
            trip.End_time = DateTime.Now;
        }

        // 右键菜单点击事件
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (!(list.SelectedItem is TripModel trip))
            {
                return;
            }

            string sql_del = "delete from trip where id=" + trip.Id;
            SQLiteCommand command = new SQLiteCommand(sql_del, MainWindow.connecttion);
            command.ExecuteNonQuery();

            ObservableCollection<TripModel> trips = DataContext as ObservableCollection<TripModel>;
            trips.RemoveAt(list.SelectedIndex);
        }
    }
}
